package com.example.pufang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.PageParamRequest;
import com.example.pufang.mapper.SystemMessageMapper;
import com.example.pufang.pojo.SystemMessage;
import com.example.pufang.pojo.SystemMessage;
import com.example.pufang.pojo.User;
import com.example.pufang.service.SystemMessageService;
import com.example.pufang.vo.ChatVo;
import com.example.pufang.vo.PageListVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SystemMessageServiceImpl extends ServiceImpl<SystemMessageMapper, SystemMessage> implements SystemMessageService {

    @Override
    public PageListVo getListByPage(PageParamRequest pageParamRequest, String searchText) {
        PageListVo pageListVo = new PageListVo();

        LambdaQueryWrapper<SystemMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(searchText), SystemMessage::getMessage, searchText)
                .orderByDesc(SystemMessage::getCreateTime);

        Page<SystemMessage> SystemMessagePage = new Page<>(pageParamRequest.getPage(), pageParamRequest.getLimit());
        IPage<SystemMessage> SystemMessageIPage = baseMapper.selectPage(SystemMessagePage , wrapper);

        List<SystemMessage> SystemMessageList = SystemMessageIPage.getRecords();


        // 为每个url添加域名
        // List<SystemMessage> SystemMessageList = SystemMessageIPage.getRecords().stream().map(e -> {
        //     if (StringUtils.isNotBlank(e.getImg())) {
        //         e.setImg(imgPath + e.getImg());
        //     }
        //     return e;
        // }).collect(Collectors.toList());

        // 获取总数
        Long total = baseMapper.selectCount(wrapper);

        // 获取总页数
        long l = total % pageParamRequest.getLimit();
        long totalPage = 0;
        if (l > 0) {
            totalPage = Math.floorDiv(total, pageParamRequest.getLimit()) + 1;
        } else {
            totalPage = Math.floorDiv(total, pageParamRequest.getLimit());
        }

        pageListVo.setPage((long) pageParamRequest.getPage())
                .setSize((long) pageParamRequest.getLimit())
                .setList(JSONObject.toJSONString(SystemMessageList))
                .setTotal(total)
                .setTotalPage(totalPage);

        return pageListVo;
    }

    @Override
    public List<ChatVo> getFrontNewMessage(User receiver) {

        ArrayList<ChatVo> list = new ArrayList<>();

        LambdaQueryWrapper<SystemMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(w -> w.eq(SystemMessage::getReceiverId, receiver.getId()).or().eq(SystemMessage::getReceiverId, -1))
                .orderByDesc(SystemMessage::getCreateTime);

        SystemMessage systemMessage = this.list(wrapper).get(0);
        ChatVo chatVo = new ChatVo();
        chatVo.setSenderId(systemMessage.getAdminId())
                .setReceiverId(receiver.getId())
                .setMessage(systemMessage.getMessage())
                        .setCreateTime(systemMessage.getCreateTime());

        list.add(chatVo);

        return list;
    }

    @Override
    public List<ChatVo> getFrontMessageList(User receiver) {

        LambdaQueryWrapper<SystemMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(w -> w.eq(SystemMessage::getReceiverId, receiver.getId()).or().eq(SystemMessage::getReceiverId, -1))
                .orderByAsc(SystemMessage::getCreateTime);

        List<ChatVo> list = this.list(wrapper).stream().map(e -> {
            ChatVo chatVo = new ChatVo();
            chatVo.setSenderId(e.getAdminId())
                    .setReceiverId(receiver.getId())
                    .setMessage(e.getMessage())
                    .setCreateTime(e.getCreateTime());

            return chatVo;
        }).collect(Collectors.toList());

        return list;
    }
}
